package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.AMapNaviViewOptions;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.NaviInfo;
import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;
import com.dhcc.android.base.utils.amap.MapUtils;
import com.dhcc.android.base.utils.amap.TTSController;

/**
 * 导航界面
 */
public class NaviCustomActivity extends SBaseActivity implements
        AMapNaviViewListener, AMapNaviListener {
    // 导航View
    private AMapNaviView mAmapAMapNaviView;
    // 导航可以设置的参数
    private boolean mDayNightFlag = MapUtils.DAY_MODE;// 默认为白天模式
    private boolean mDeviationFlag = MapUtils.YES_MODE;// 默认进行偏航重算
    private boolean mJamFlag = MapUtils.YES_MODE;// 默认进行拥堵重算
    private boolean mTrafficFlag = MapUtils.OPEN_MODE;// 默认进行交通播报
    private boolean mCameraFlag = MapUtils.OPEN_MODE;// 默认进行摄像头播报
    private boolean mScreenFlag = MapUtils.YES_MODE;// 默认是屏幕常亮
    // 导航界面风格
    private int mThemeStle;
    private View main;
    private Bundle savedInstanceState;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        setContentView(R.layout.task_navi_navicustom);
    }

    /**
     * 设置导航的参数
     */
    private void setAmapNaviViewOptions() {
        if (mAmapAMapNaviView == null) {
            return;
        }
        AMapNaviViewOptions viewOptions = new AMapNaviViewOptions();
        viewOptions.setSettingMenuEnabled(false);// 设置导航setting可用
        viewOptions.setNaviNight(mDayNightFlag);// 设置导航是否为黑夜模式
        viewOptions.setReCalculateRouteForYaw(mDeviationFlag);// 设置导偏航是否重算
        viewOptions.setReCalculateRouteForTrafficJam(mJamFlag);// 设置交通拥挤是否重算
        viewOptions.setTrafficInfoUpdateEnabled(mTrafficFlag);// 设置是否更新路况
        viewOptions.setCameraInfoUpdateEnabled(mCameraFlag);// 设置摄像头播报
        viewOptions.setScreenAlwaysBright(mScreenFlag);// 设置屏幕常亮情况
        viewOptions.setNaviViewTopic(mThemeStle);// 设置导航界面主题样式

        mAmapAMapNaviView.setViewOptions(viewOptions);
    }

    // -----------------------------导航界面回调事件------------------------

    /**
     * 导航界面返回按钮监听
     */
    @Override
    public void onNaviCancel() {
        goBack();
    }

    @Override
    public void onNaviSetting() {

    }

    @Override
    public void onNaviMapMode(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNaviTurnClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNextRoadClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onScanViewButtonClick() {
        // TODO Auto-generated method stub

    }

    /**
     * 返回键监听事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void goBack() {
        if (StringUtil.isFastDoubleClick(500)) {
            return;
        }
        Intent intent = new Intent(this, NaviMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    // ------------------------------生命周期方法---------------------------
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mAmapAMapNaviView.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAmapAMapNaviView.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
        mAmapAMapNaviView.onPause();
        AMapNavi.getInstance(this).stopNavi();
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("导航");
    }

    @Override
    protected void initView() {
        mAmapAMapNaviView = (AMapNaviView) findViewById(R.id.customnavimap);
        mAmapAMapNaviView.onCreate(savedInstanceState);
        // mAmapAMapNaviView.setAMapNaviViewListener(this);
        TTSController.getInstance(this).startSpeaking();
        // 开启实时导航
        AMapNavi.getInstance(this).startNavi(AMapNavi.GPSNaviMode);
        AMapNavi.getInstance(this).setAMapNaviListener(this);
        setAmapNaviViewOptions();
        setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();
            }
        });
    }

    @Override
    public void onDestroy() {
        new MSApplication().deleteActivity(this);
        mAmapAMapNaviView.onDestroy();
        TTSController.getInstance(this).stopSpeaking();
        super.onDestroy();
    }

    @Override
    public void onLockMap(boolean arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onArriveDestination() {
        Intent intent = new Intent(NaviCustomActivity.this,
                NaviLineActivity.class);
        intent.putExtra("endLon", Constants.TargetXY[1]);
        intent.putExtra("endLat", Constants.TargetXY[0]);
        startActivity(intent);
        TTSController.getInstance(this).playText("到达目的地");
    }

    @Override
    public void onArrivedWayPoint(int arg0) {
    }

    @Override
    public void onCalculateRouteFailure(int arg0) {
        TTSController.getInstance(this).playText("路径计算失败，请检查网络或输入参数");
    }

    @Override
    public void onCalculateRouteSuccess() {
        String calculateResult = "路径计算就绪";

        TTSController.getInstance(this).playText(calculateResult);
    }

    @Override
    public void onEndEmulatorNavi() {
        TTSController.getInstance(this).playText("导航结束");

    }

    @Override
    public void onGetNavigationText(int arg0, String arg1) {
        // TODO Auto-generated method stub
        System.out.println("内容：" + arg1);
        TTSController.getInstance(this).playText(arg1);
    }

    @Override
    public void onInitNaviFailure() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onInitNaviSuccess() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChange(AMapNaviLocation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {
        // TODO Auto-generated method stub
        TTSController.getInstance(this).playText("前方路线拥堵，路线重新规划");
    }

    int i = 0;

    @Override
    public void onReCalculateRouteForYaw() {

        // 可以在频繁重算时进行设置,例如五次之后
        i++;
        if (i >= 5) {
            AMapNaviViewOptions viewOptions = new AMapNaviViewOptions();
            viewOptions.setReCalculateRouteForYaw(false);
            mAmapAMapNaviView.setViewOptions(viewOptions);
            TTSController.getInstance(this).playText("您已偏航超过五次，现在为您重新规划路线");
        } else {
            TTSController.getInstance(this).playText("您已偏航");
        }
    }

    @Override
    public void onStartNavi(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTrafficStatusUpdate() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGpsOpenStatus(boolean arg0) {
        // TODO Auto-generated method stub
        if (!arg0) {
            TTSController.getInstance(this).playText("请打开GPS功能");
        }

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo arg0) {

        // TODO Auto-generated method stub

    }

}
